package Zoo.handler;

/**
 * 动物饲养员增加动物界面处理类
 */

import Zoo.entity.AnimalEntity;
import Zoo.impl.ZoomenService1Impl;
import Zoo.service.ZoomenService1;
import Zoo.view.zoomanview.AddAnimalView;
import Zoo.view.zoomanview.ZoomenView1;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddAnimalViewHandler implements ActionListener {
    private AddAnimalView addAnimalView;
    private ZoomenView1 zoomenView1;

    public AddAnimalViewHandler(AddAnimalView addAnimalView, ZoomenView1 zoomenView1) {
        this.addAnimalView = addAnimalView;
        this.zoomenView1 = zoomenView1;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
        String text = jButton.getText();
        if ("添加".equals(text)) {
            ZoomenService1 zoomenService1 = new ZoomenService1Impl();
            AnimalEntity animalEntity = addAnimalView.buildAnimalEntity();
            boolean addResult = zoomenService1.add(animalEntity);
            if (addResult) {
                //重新加载表格查到最新数据
                zoomenView1.reloadTable();
                addAnimalView.dispose();
            } else {
                JOptionPane.showMessageDialog(addAnimalView, "添加失败");

            }

        }
    }

}
